package net.ezsteam.database;

import java.util.ArrayList;
import java.util.List;

import net.ezsteam.object.EzSteam_Vehicle;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class EzSteam_DatabaseHandler extends SQLiteOpenHelper {

    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "ezsteam";

    // Contacts table name
    private static final String TABLE_VEHICLE = "vehicles";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_MAKE= "make";
    private static final String KEY_MODEL = "model";
    private static final String KEY_YEAR = "year";

    public EzSteam_DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_VEHICLE + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_MAKE + " TEXT,"
                + KEY_MODEL + " TEXT" + KEY_YEAR+ " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_VEHICLE);

        onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */

    void addCar(EzSteam_Vehicle contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_MAKE, contact.getMake());
        values.put(KEY_MODEL, contact.getModel()); 
        values.put(KEY_YEAR, contact.getYear());
        db.insert(TABLE_VEHICLE, null, values);
        db.close();
    }

    EzSteam_Vehicle getCar(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_VEHICLE, new String[] { KEY_ID,
                KEY_MAKE, KEY_MODEL , KEY_YEAR }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        EzSteam_Vehicle contact = new EzSteam_Vehicle(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), cursor.getString(2), cursor.getString(3));
        return contact;
    }

    public List<EzSteam_Vehicle> getAllCars() {
        List<EzSteam_Vehicle> contactList = new ArrayList<EzSteam_Vehicle>();

        String selectQuery = "SELECT  * FROM " + TABLE_VEHICLE;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
            	EzSteam_Vehicle contact = new EzSteam_Vehicle();
                contact.setId(Integer.parseInt(cursor.getString(0)));
                contact.setMake(cursor.getString(1));
                contact.setModel(cursor.getString(2));
                contact.setYear(cursor.getString(3));

                contactList.add(contact);
            } while (cursor.moveToNext());
        }

        return contactList;
    }

    public int updateCar(EzSteam_Vehicle car) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_MAKE, car.getMake());
        values.put(KEY_MODEL, car.getModel());
        values.put(KEY_YEAR, car.getYear());

        return db.update(TABLE_VEHICLE, values, KEY_ID + " = ?",
                new String[] { String.valueOf(car.getId()) });
    }

    public void deleteCar(EzSteam_Vehicle car) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_VEHICLE, KEY_ID + " = ?",
                new String[] { String.valueOf(car.getId()) });
        db.close();
    }


    public int getContactsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_VEHICLE;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        return cursor.getCount();
    }

}
